package com.yunxi.service.dubboService.impl.sys;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.customer.pojo.CustomCreditReportingVO;
import com.yunxi.model.sys.SysAttach;
import com.yunxi.order.pojo.OrderCustomerVO;
import com.yunxi.order.service.OrderCustomerDubboService;
import com.yunxi.service.localService.customer.CustomCreditReportingService;
import com.yunxi.service.localService.sys.AttachService;
import com.yunxi.sys.pojo.AttachVO;
import com.yunxi.sys.service.AttachDubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-07
 */
/*@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)*/
public class AttachDubboServiceImpl implements AttachDubboService {

    @Autowired
    @Qualifier(value = "attachServiceImpl")
    AttachService sysAttachService;

    @Autowired
    OrderCustomerDubboService orderCustomerDubboServiceBase;

   @Autowired
   @Qualifier(value = "customCreditReportingServiceImpl")
   CustomCreditReportingService customCreditReportingService;

    @Override
    public AttachVO getById(String id) throws InvocationTargetException, IllegalAccessException {
        SysAttach sysAttach = sysAttachService.getById(id);
        AttachVO sysAttachVo = new AttachVO();
        BeanTools.copyProperties(sysAttachVo,sysAttach);
        return sysAttachVo;
    }

    @Override
    public void saveOrUpdate(AttachVO sysAttachVo) throws InvocationTargetException, IllegalAccessException {
        SysAttach sysAttach = new SysAttach();
        BeanTools.copyProperties(sysAttach,sysAttachVo);
        sysAttachService.saveOrUpdate(sysAttach);
    }

    @Override
    public void removeById(String id) {
    sysAttachService.removeById(id);
    }

    @Override
    public List<AttachVO> findAll(String pageNum, String pageSize) throws InvocationTargetException, IllegalAccessException {
        List<SysAttach> sysAttachList = sysAttachService.findAll(pageNum,pageSize);

        List<AttachVO> sysAttachVoList = new ArrayList<>();

        for (SysAttach sysAttach : sysAttachList) {
            AttachVO sysAttachVo = new AttachVO();
            BeanTools.copyProperties(sysAttachVo,sysAttach);
            sysAttachVoList.add(sysAttachVo);
        }
        return sysAttachVoList;

   }

    @Override
    public PageInfo<AttachVO> findPageByVO(AttachVO attachVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
        PageInfo<AttachVO> pageInfo = sysAttachService.findPageByVO(attachVO,pageNum,pageSize);
        return pageInfo;
    }

    @Override
    public List listByVo(AttachVO vo) {
        return sysAttachService.listByVo(vo);
    }

    @Override
    public void removeByListVo(List<AttachVO> sysAttachVoList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        sysAttachService.removeByListVo(sysAttachVoList);
    }

   @Override
   @Transactional
   public void save(List<AttachVO> attachVOList) throws Exception {
       if (attachVOList!=null && attachVOList.size()>0){
          OrderCustomerVO orderCustomerVO = orderCustomerDubboServiceBase.getByOrderCustomerNo(attachVOList.get(0).getBusSubId());
          List<CustomCreditReportingVO> list = customCreditReportingService.getByOrderCustomerNo(orderCustomerVO.getOrderCustomerNo());
          for (AttachVO attachVO : attachVOList) {
             //如果上传的为订单合同图片修改订单上传状态
             if ("pms_order_customer".equals(attachVO.getBusId())){
                if (list!=null && list.size()>0){
                   CustomCreditReportingVO customCreditReportingVO = list.get(0);
                   if (orderCustomerVO.getBuyType()==2){
                      if (customCreditReportingVO.getApprovalStatus().intValue() !=2) throw new Exception("当前体验车订单用户征信尚未审核通过,无法上传合同照片");
                   }else if (orderCustomerVO.getBuyType()==1){
                      if (customCreditReportingVO.getApprovalStatus().intValue() !=5) throw new Exception("当前订单金融方案用户尚未确认,无法上传合同照片");
                   }
                }else {
                   if (orderCustomerVO.getBuyType()==2) throw new Exception("当前体验车订单尚未上传征信信息,无法上传合同照片");
                }
                orderCustomerDubboServiceBase.updateByOrderCustomerNo(attachVO.getBusSubId());
             }
             SysAttach sysAttach = new SysAttach();
             BeanUtils.copyProperties(attachVO,sysAttach);
             sysAttachService.save(sysAttach);
          }
          if ("pms_order_customer".equals(attachVOList.get(0).getBusId())){
             ApiResult result = orderCustomerDubboServiceBase.sendOrderStatus2App(orderCustomerVO, "OC01_OD_1000","1002");
             if (result!=null) throw new Exception(result.getMsg());
          }
       }
   }

    @Override
    public void saveByAttachVoList(List<AttachVO> attachVOList, String bus_id, String busSubId, LoginInfo loginInfo) {
        sysAttachService.saveByAttachVoList(attachVOList,bus_id,busSubId,loginInfo);
    }

   @Override
   public void delByBusSubId(String busId, String partnerCode) {
      sysAttachService.delByBusSubId(busId,partnerCode);
   }
}
